package com.fanghuaiming.config.datasource;

import com.fanghuaiming.config.model.XaLogDataSource;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.jta.atomikos.AtomikosDataSourceBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

/**
 * @author fanghuaiming
 */
@Configuration
@MapperScan(basePackages = LogDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "logSqlSessionFactory",sqlSessionTemplateRef = "logSqlSessionTemplate")
public class LogDataSourceConfig {

    static final String PACKAGE = "com.fanghuaiming.dao.mapper.log";

    @Autowired
    private XaLogDataSource xaLogDataSource;

    /**
     * @Description: 创建dataSource
     *
     * @param:
     * @return:
     * @auther: fanghuaiming
     * @date: 2020/11/25 下午5:09
     * @version:1.0.0
     */
    @SuppressWarnings("all")
    /*@Bean(name = "logDataSource")
    public DataSource logDataSource() {
        *//*MysqlXADataSource mysqlXADataSource = new MysqlXADataSource();
        mysqlXADataSource.setUrl(xaLogDataSource.getJdbcUrl());
        mysqlXADataSource.setPinGlobalTxToPhysicalConnection(true);
        mysqlXADataSource.setUser(xaLogDataSource.getUsername());
        mysqlXADataSource.setPassword(xaLogDataSource.getPassword());

        AtomikosDataSourceBean atomikosDataSourceBean = new AtomikosDataSourceBean();
        atomikosDataSourceBean.setXaDataSource(mysqlXADataSource);
        atomikosDataSourceBean.setUniqueResourceName("logDataSource");
        atomikosDataSourceBean.setMinPoolSize(xaLogDataSource.getMinIdle());
        return mysqlXADataSource;*//*

        AtomikosDataSourceBean atomikosDataSourceBean = new AtomikosDataSourceBean();
        atomikosDataSourceBean.setUniqueResourceName("logDataSource");
        atomikosDataSourceBean.setXaDataSourceClassName("com.mysql.jdbc.jdbc2.optional.MysqlXADataSource");
        Properties properties = new Properties();
        properties.put("URL", xaLogDataSource.getJdbcUrl());
        properties.put("user", xaLogDataSource.getUsername());
        properties.put("password", xaLogDataSource.getPassword());
        atomikosDataSourceBean.setXaProperties(properties);
        return atomikosDataSourceBean;
    }*/

    @Bean(name = "logDataSource")
    public DataSource logDataSource() {
        /*MysqlXADataSource dataSource = new MysqlXADataSource();
        dataSource.setURL("jdbc:mysql:///multi_ds01");
        dataSource.setUser("root");
        dataSource.setPassword("root");
        dataSource.setPinGlobalTxToPhysicalConnection(true);*/

        AtomikosDataSourceBean atomikosDataSourceBean = new AtomikosDataSourceBean();
        atomikosDataSourceBean.setUniqueResourceName("logDataSource");
        atomikosDataSourceBean.setXaDataSourceClassName("com.mysql.jdbc.jdbc2.optional.MysqlXADataSource");
        //1、设置druid方式
        //atomikosDataSourceBean.setXaDataSourceClassName("com.alibaba.druid.pool.xa.DruidXADataSource");
        //atomikosDataSourceBean.setXaDataSource(new DruidXADataSource());
        //2、设置Oracle方式
        //atomikosDataSourceBean.setXaDataSourceClassName("oracle.jdbc.driver.OracleDriver");
        //3、设置hikari方式
        //atomikosDataSourceBean.setXaDataSourceClassName("com.zaxxer.hikari.HikariDataSource");
        Properties properties = new Properties();
        properties.put("URL", xaLogDataSource.getJdbcUrl());
        properties.put("user", xaLogDataSource.getUsername());
        properties.put("password", xaLogDataSource.getPassword());
        atomikosDataSourceBean.setXaProperties(properties);
        return atomikosDataSourceBean;
    }

    /**
     * @Description: 创建transactionManager
     *
     * @param:
     * @return:
     * @auther: fanghuaiming
     * @date: 2020/11/25 下午5:09
     * @version:1.0.0
     */
    /*@Bean(name = "logTransactionManager")
    public DataSourceTransactionManager logTransactionManager() {
        return new DataSourceTransactionManager(this.logDataSource());
    }*/

    /**
     * @Description: 创建SqlSessionTemplate
     *
     * @param:
     * @return:
     * @auther: fanghuaiming
     * @date: 2020/11/25 下午5:09
     * @version:1.0.0
     */
    @Bean(name = "logSqlSessionTemplate")
    public SqlSessionTemplate logSqlSessionTemplate(@Qualifier("logSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

    /**
     * @Description: 创建logSqlSessionFactory
     *
     * @param:
     * @return:
     * @auther: fanghuaiming
     * @date: 2020/11/25 下午5:10
     * @version:1.0.0
     */
    @Bean(name = "logSqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("logDataSource") DataSource dataSource) throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(dataSource);
        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mappers/log/*.xml"));
        sessionFactory.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
        return sessionFactory.getObject();
    }

}
